function Map = fsb_colmap(Map)

% FSB: Unified colormap routines
%
% EXAMPLE: [zi,tzi,cm,zm] = fsb_colmap(z,corparam)
%
% INPUT:
% Map: input argument struct with the fields:
% Map.z : Slice containing results of statistical tests
% Map.corparam: Threshold of image
% Map.probparam: Threshold of image
% Map.rc : which values are going to be displayed
%               (0: Probabilities; 1 : Correlation)
%
% OUTPUT:
% Map: output struct with the fields:
% Map.tzi : Map alpha data
% Map.zm: Actual map image
% Map.z : Slice containing results of statistical tests
% Map.cm : Map colormap
%
% CALLED BY:
% FSB.m
% fsb_functional_map.m
%
% NOTES:
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer, jointly developed with Andreas Bartels
% License:GNU GPL, no express or implied warranties
% 
% $ Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

z = Map.z;
corparam = Map.corparam;

switch Map.rc

    case 1

        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        % Colormap type a
        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        cmp = hot(50);
        cmn = jet(100);
        cm = [flipud(cmn(1:51,:));(cmp)];
        cm (50,:)=[0 0 0];
        cm (51,:)=[0 0 0];
        z(abs(z)<corparam)=0;
        tzi = abs(z);
        zi=round(z*50)+50;
        zi=max(zi,1); % set all vals < 0 to 1
        zm = cm(zi(:),:);
        zm = reshape(zm,[size(z) 3]);
        Map.tzi = ceil(tzi);

    case 0

        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        % Colormap type b
        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        cm = hot(100);
        cm (100,:)=[0 0 0];
        zi = abs(log10(z));
        zi=round(zi*10);
        zi(zi>100)=100;
        zi=max(zi,1); % set all vals < 0 to 1
        zm = cm(zi(:),:);
        zm = reshape(zm,[size(z) 3]);
        Map.tzi = ceil(z);

    case 2

        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        % Colormap type c
        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        cm = hot(100);
        cm (100,:)=[0 0 0];
        z(z>probparam)=0;
        zi = abs(log10(z));
        zi=round(zi*20); % This is the only difference to colormap type b
        zi(zi>100)=100;
        zi=max(zi,1); % set all vals < 0 to 1
        zm = cm(zi(:),:);
        zm = reshape(zm,[size(z) 3]);
        Map.tzi = ceil(z);
        
    case 99

        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        % Colormap type ICA - scaling is not so simple...
        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        cmp = hot(50); % Take the hot colormap for positive data
        cmn = jet(100); % take the jet colormap for negative data
        cmn = flipud(cmn(1:51,:)); % flip and truncate the jet colormap to make it look better
        cm = [cmn; cmp]; % concatenate both colormaps
        cm (50:51,:)=[0 0 0;0 0 0]; % take out values 50 and 51 from the colormap
        z(abs(z)<corparam)=0; % threshold the statistical map
        z = z/max(z(:));
        tzi = abs(z); % get only positive values from z
        zi=round(z*50)+50; % Normalize values to [-1:+1]
        zi=max(zi,1); % set all vals < 0 to 1
        zit = zi(:);
        zm = cm(zit,:); % get an index of the colormap from 
        zm = reshape(zm,[size(z) 3]);
        Map.tzi = ceil(tzi);

end

Map.zm = zm;
Map.cm = cm;

end
